/**
 * description 获取某个月份的天数
 * @param year
 * @param month
 * @returns
 */
export const daysOfMonth = (year: number, month: number) => {
  return new Date(year, month + 1, 0).getDate();
};

/**
 * @description 获取某个月份的第一天是周几
 * @param year
 * @param month
 * @returns
 */
export const firstDayOfMonth = (year: number, month: number) => {
  return new Date(year, month, 1).getDay();
};

/**
 * @description 处理当前是当前最后一天时切换月份的情况
 * @param date
 * @returns
 */
export const handleChangeMonthForLastDay = (
  date: Date,
  type: "prev" | "next"
): number => {
  // 获取上月有多少天
  const daysCount = daysOfMonth(
    date.getFullYear(),
    type === "prev" ? date.getMonth() - 1 : date.getMonth() + 1
  );
  let day = date.getDate();
  if (date.getDate() > daysCount) {
    day = daysCount;
  } else {
    const curMonthDays = daysOfMonth(date.getFullYear(), date.getMonth());
    if (curMonthDays === date.getDate()) {
      day = daysCount;
    } else {
      day = date.getDate();
    }
  }
  return day;
};

/**
 * @description 获取当月最后一天是周几
 * @param year
 * @param month
 */
export const lastDayOfMonth = (year: number, month: number) => {
  return new Date(year, month + 1, 0).getDay();
};
